const { ccclass, property } = cc._decorator;
import loadLobby from '../Load/loadLobby';
import skillItemControl from './skillItemControl';
import WinNodeBase from '../WinNodeBase';
import skillLoadControl from '../Lobby/skillLoadControl';
import WinFormBase from '../WinFormBase';
import LotteryControl from './LotteryControl';
import SoundMgr from '../SoundMgr';

@ccclass
export default class skillPageControl extends WinNodeBase {
    @property(cc.Node)
    Lottery: cc.Node = null;
    @property(cc.Node)
    loadSkill: cc.Node = null;
    @property(cc.Node)
    contentNode: cc.Node = null;
    @property(cc.Node)
    SoundNode: cc.Node = null;

    _skillItemPfb: cc.Prefab = null;
    _SkillIcons: cc.SpriteAtlas = null;
    _SkillsJson: any = null;
    Attribute: string[] = ["普通", "火系魔法", "毒系魔法", "水系魔法", "风系魔法"];
    _indexRandom: number = 0;
    _SoundMgr: SoundMgr = null;

    onLoad() {
        this._SoundMgr = this.SoundNode.getComponent(SoundMgr);
        this.getDeteil();
    }
    getDeteil() {
        //获取常驻节点所绑定的脚本 
        let _loadLobby = cc.director.getScene().getChildByName('loadLobby').getComponent(loadLobby);
        if (_loadLobby) {
            this._SkillIcons = _loadLobby.SkillIcons;
            this._SkillsJson = _loadLobby.SkillsJson.SkillHs;
            this._skillItemPfb = _loadLobby.skillItemPfb;
        }
    }
    private getParam(id: string): any {
        for (let index = 0; index < this._SkillsJson.Hit.length; index++) {
            const element = this._SkillsJson.Hit[index];
            if (element.Id == id)
                return element;
        }
        for (let index = 0; index < this._SkillsJson.Put.length; index++) {
            const element = this._SkillsJson.Put[index];
            if (element.Id == id)
                return element;
        }
        for (let index = 0; index < this._SkillsJson.Self.length; index++) {
            const element = this._SkillsJson.Self[index];
            if (element.Id == id)
                return element;
        }
    }
    private setLoad() {
        let date: string[] = gameHelper.getInstance().skillParam.a;
        let i = 0;
        date.forEach(element => {
            if (element)
                this.setLoadItem(element, i);
            i++;
        });
    }
    private addLoad(id: string): number {
        let date: string[] = gameHelper.getInstance().skillParam.a;
        for (let index = 0; index < date.length; index++) {
            const element = date[index];
            if (!element) {
                this.setLoadItem(id, index);
                return index;
            }
        }
        return -1;
    }
    setLoadItem(id: string, index: number) {
        let node = this.loadSkill.children[index];
        let param = this.getParam(id);
        node.getComponent(cc.Sprite).spriteFrame = this._SkillIcons.getSpriteFrame(param.Icon);
        let contr = node.getComponent(skillLoadControl);
        contr._SkillId = id;
        contr.title.string = param.Title;
        contr._skillPage = this;
        contr._SoundMgr = this._SoundMgr;
        contr.init();
    }
    private setContent() {
        let date: string[] = gameHelper.getInstance().skillParam.b;
        date.forEach(element => {
            this.setContentItem(element);
        });
    }
    setContentItem(id: string) {
        if (!id || id == "") {
            this._indexRandom++;
            let node = cc.instantiate(this._skillItemPfb);
            node.on(cc.Node.EventType.TOUCH_START, this.Unload, this);
            node.name = "--";
            let contr = node.getComponent(skillItemControl);
            contr._SkillId = this._indexRandom.toString();
            //contr._SoundMgr = this._SoundMgr;
            this.contentNode.addChild(node);
        }
        else {
            let node: cc.Node = null;
            let nodes: cc.Node[] = this.contentNode.children.filter(item => item.name == id);
            if (nodes?.length > 0) {
                node = nodes[0];
                let contr = node.getComponent(skillItemControl);
                contr._count++;
                contr.showCount();
            }
            else {
                node = cc.instantiate(this._skillItemPfb);
                node.on(cc.Node.EventType.TOUCH_START, this.Unload, this);
                node.name = id;
                let param = this.getParam(id);
                let contr = node.getComponent(skillItemControl);
                //contr._SoundMgr = this._SoundMgr;
                contr._SkillId = id;
                contr.icon.spriteFrame = this._SkillIcons.getSpriteFrame(param.Icon);
                contr.title.string = param.Title;
                contr.explain.string = param.Explain;
                if (param.Attribute)
                    contr.attribute.string = this.Attribute[param.Attribute];
                else
                    contr.attribute.string = this.Attribute[0];
                this.contentNode.addChild(node);
            }
        }
    }
    Unload(event) {
        let node: cc.Node = event.target;
        let _id = node.name;
        if (_id != "--") {
            let index = this.addLoad(_id);
            if (index > - 1) {
                let param = gameHelper.getInstance().skillParam;
                param.a[index] = _id;
                let _index = (param.b as string[]).indexOf(_id);
                (param.b as string[]).splice(_index, 1);
                gameHelper.getInstance().skillParam = param;

                let control = node.getComponent(skillItemControl);
                if (control._count > 1) {
                    control._count--;
                    control.showCount();
                }
                else {
                    node.destroy();
                }
            }
        }
        else {
            this.setLottery(node);
        }
        this._SoundMgr.play("click");
        return;
    }
    setLottery(_node: cc.Node) {
        this._SoundMgr.play("click1");
        let param = null;
        do {
            param = this.randomParam();
        } while (!param);
        let contr = _node.getComponent(skillItemControl);
        let dbParam = gameHelper.getInstance().skillParam;

        let _count = parseInt(contr._SkillId);
        let count = 0;
        for (let index = 0; index < dbParam.b.length; index++) {
            const element = dbParam.b[index];
            if (!element) {
                count++;
                if (_count == count) {
                    dbParam.b[index] = param.Id;
                    break;
                }
            }
        }
        gameHelper.getInstance().skillParam = dbParam;

        _node.name = param.Id;
        contr._SkillId = param.Id;
        contr.icon.spriteFrame = this._SkillIcons.getSpriteFrame(param.Icon);
        contr.title.string = param.Title;
        contr.explain.string = param.Explain;
        if (param.Attribute)
            contr.attribute.string = this.Attribute[param.Attribute];
        else
            contr.attribute.string = this.Attribute[0];

        let lcontrol = this.Lottery.getComponent(LotteryControl)
        lcontrol.label.string = param.Title;
        lcontrol.prizeSprite.spriteFrame = this._SkillIcons.getSpriteFrame(param.Icon);

        let fromNode = new cc.Node();
        let fromControl = fromNode.addComponent(WinFormBase);
        fromControl.formIndex = 2;//二级窗口
        fromControl.mainNode = this.Lottery;
        fromControl.bgNode = this.FormBg;
        this.node.addChild(fromNode);
    }
    randomParam(): any {
        let type = Math.floor(Math.random() * 3);
        switch (type) {
            case 0:
                {
                    if (this._SkillsJson.Hit?.length > 0) {
                        let index = Math.floor(Math.random() * this._SkillsJson.Hit.length);
                        return this._SkillsJson.Hit[index];
                    }
                    break;
                }
            case 1:
                {
                    if (this._SkillsJson.Put?.length > 0) {
                        let index = Math.floor(Math.random() * this._SkillsJson.Put.length);
                        return this._SkillsJson.Put[index];
                    }
                    break;
                }
            default:
                {
                    if (this._SkillsJson.Self?.length > 0) {
                        let index = Math.floor(Math.random() * this._SkillsJson.Self.length);
                        return this._SkillsJson.Self[index];
                    }
                    break;
                }
        }
        return null;
    }
    onEnable() {
        this.setLoad();
        this.setContent();
    }
    onDisable() {
        this.contentNode.removeAllChildren();
    }
}
